package Work2;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;

public class MoraGame {
	public static void main(String[] args) {
		GameBegin();
	}
	
	public static void GameBegin() {
		System.out.println("---猜拳游戏---");
		System.out.println("五局三胜制");
		int playerwin=0,pcwin=0,count=0;
		ArrayList<Integer> player = new ArrayList<Integer>();
		while(count<5) {
			int playerchoice=PlayerChoice();
			int pcchoice=AIChoice(player,playerwin,pcwin);
			int win=Iswin(playerchoice,pcchoice);
			if(win==1) {
				playerwin++;
			}else if(win==2) {
				pcwin++;
			}
			player.add(playerchoice);
			count++;
			if(playerwin==3) {
				break;
			}else if(pcwin==3) {
				break;
			}
		}
		if(playerwin>pcwin) {
			System.out.println("游戏结束，玩家胜利");
		}else if(pcwin>playerwin) {
			System.out.println("游戏结束，电脑胜利");
		}else if(pcwin==playerwin) {
			System.out.println("游戏结束，平局");
		}
	}
	
	public static String Changenumtostr(int num) {
		if(num==1) {
			return "石头";
		}else if(num==2) {
			return "剪刀";
		}else if(num==3) {
			return "布";
		}
		return "ERROR";
	}
	
	public static int Iswin(int player,int pc) {
		String player1=Changenumtostr(player);
		String pc1=Changenumtostr(pc);
		if(player == pc) {
			System.out.println("玩家:"+player1+"  电脑:"+pc1);
			System.out.println("本轮平局");
			return 0;
		}else if ((player==1 && pc==2) || (player==2 && pc==3) || (player==3 && pc==1)) {
			System.out.println("玩家:"+player1+"  电脑:"+pc1);
			System.out.println("本轮玩家胜利");
			return 1;
		}else {
			System.out.println("玩家:"+player1+"  电脑:"+pc1);
			System.out.println("本轮电脑胜利");
			return 2;
		}
	}
	
	public static int PlayerChoice() {
		Scanner sc = new Scanner(System.in);
		int choice;
		System.out.println("请输入你的选择1.石头 2.剪刀 3.布(请输入数字):");
		while(true) {
			choice = sc.nextInt();
			if(choice!=1&&choice!=2&&choice!=3) {
				System.out.println("输入错误，请输入数字1、2、3:");
			}else {
				return choice;
			}
		}
	}
	
	public static int AIChoice(ArrayList<Integer> player,int winplayer,int winpc) {
		Random rd = new Random();
		if(winplayer>winpc) {
			int doit = rd.nextInt(2);
			if(doit==0) {
				return AIStrategy(player);
			}else {
				return rd.nextInt(3)+1;
			}
		}else {
			return rd.nextInt(3)+1;
		}
	}
	
	public static int AIStrategy(ArrayList<Integer> player) {
		int rock=0,scissor=0,paper=0;
		Random rd = new Random();
		for(int i=0;i<player.size();i++) {
			if(player.get(i)==1) {
				rock++;
			}else if(player.get(i)==2) {
				scissor++;
			}else if(player.get(i)==3) {
				paper++;
			}
		}
		if(rock>scissor&&rock>paper) {
			return rd.nextInt(2)+1;
		}else if(scissor>rock&&scissor>paper) {
			return rd.nextInt(2)+2;
		}else if(paper>rock&&paper>scissor) {
			int choice=rd.nextInt(2);
			if(choice==0) {
				return 1;
			}else {
				return 3;
			}
		}else {
			return rd.nextInt(3)+1;
		}
	}
}
